package com.dai.zhenfujin.visitingcard.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.dai.zhenfujin.R;
import com.dai.zhenfujin.addresslist.fragment.AttentionPagerFragment;
import com.dai.zhenfujin.addresslist.fragment.FansPagerFragment;
import com.dai.zhenfujin.base.BaseActivity;
import com.dai.zhenfujin.visitingcard.bean.Image;

import java.util.ArrayList;
import java.util.List;

/**
 * 粉丝福利
 */
public class FansActivity extends BaseActivity implements View.OnClickListener {

    private TextView tvName, tv_alum;
    private FrameLayout fl_alum;
    private ImageView imageView,iv_alum;
    private ListView listView;
    private ViewPager viewPager;
    private RadioGroup radioGroup;

    private List<List<Image>> imagesList;
    private List<Fragment> fragments;

    private String[][] images = new String[][]{{"http://t2.27270.com/uploads/tu/201510/249/8.jpg", "640", "960"}
            , {"http://t2.27270.com/uploads/tu/201606/76/32.jpg", "640", "640"}
            , {"http://t2.27270.com/uploads/tu/201510/249/9.jpg", "640", "640"}
            , {"http://t2.27270.com/uploads/tu/201606/112/17.jpg", "640", "640"}
            , {"http://t2.27270.com/uploads/tu/201510/249/3.jpg", "640", "640"}
            , {"http://t2.27270.com/uploads/tu/201606/62/28.jpg", "640", "640"}
            , {"http://t2.27270.com/uploads/tu/201606/76/34.jpg", "640", "640"}
            , {"http://t2.27270.com/uploads/tu/201606/73/slt.jpg", "640", "640"}
            , {"http://img2.imgtn.bdimg.com/it/u=3347259689,1828160575&fm=21&gp=0.jpg", "640", "640"}
            , {"http://img1.imgtn.bdimg.com/it/u=3607821315,1190508392&fm=21&gp=0.jpg", "640", "640"}
            , {"http://img4.imgtn.bdimg.com/it/u=2495945657,2561148855&fm=21&gp=0.jpg", "640", "640"}
            , {"http://t2.27270.com/uploads/tu/201510/249/7.jpg", "800", "650"}};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fans);
        viewPager = findViewById(R.id.addresslist_pager);
        radioGroup = findViewById(R.id.addresslist_rg);
        radioGroup.check(R.id.addresslist_rb1);

        imageView = findViewById(R.id.imageview);
        fl_alum = findViewById(R.id.fl_alum);
        tv_alum = findViewById(R.id.tv_alum);
        iv_alum = findViewById(R.id.iv_alum);
        initListener();
        initData();

        // listView = findViewById(R.id.fans_listview);

        imagesList = new ArrayList<>();
        for (int i = 0; i < images.length; i++) {
            ArrayList<Image> itemList = new ArrayList<>();
            for (int j = 0; j <= i; j++) {
                itemList.add(new Image(images[j][0], Integer.parseInt(images[j][1]), Integer.parseInt(images[j][2])));
            }
            imagesList.add(itemList);
        }
        //  final FansAdapter adapter = new FansAdapter(this, imagesList);
        //  listView.setAdapter(adapter);
    }

    private void initData() {
        fragments = new ArrayList<>();
        fragments.add(new AttentionPagerFragment());
        fragments.add(new FansPagerFragment());
        //viewpaer一次加载全部页面
        viewPager.setOffscreenPageLimit(fragments.size());

        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
    }


    private void initListener() {
        imageView.setOnClickListener(this);
        fl_alum.setOnClickListener(this);
        iv_alum.setOnClickListener(this);
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.addresslist_rb1:
                    viewPager.setCurrentItem(0);
                    break;
                case R.id.addresslist_rb2:
                    viewPager.setCurrentItem(1);
                    break;
            }
        });

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                //实现滑动页面下方按钮的联动
                radioGroup.check(radioGroup.getChildAt(i).getId());
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_alum:
                Intent intent = new Intent(this, AlumActivity.class);
                startActivity(intent);
                break;
            case R.id.imageview:
                finish();
                break;
        }
    }

    //适配器
    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}
